package me.zk.practice1.s;

import java.util.Arrays;

/**
 * @author zhangkai
 * @date 2023/7/6 10:01
 */
public class LS88 {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        if (n >= 0) System.arraycopy(nums2, 0, nums1, m, n);
        Arrays.sort(nums1);
    }



    public static void merge2(int[] nums1, int m, int[] nums2, int n) {
       int[] temp = new int[m+n];
       int i = 0;
       int j = 0;
        for (int k = 0; k < m+n; k++) {
            if (i >=m) {
                temp[k] = nums2[j];
                j++;
            } else if (j>=n) {
                temp[k] = nums1[i];
                i++;
            } else if(nums1[i] <= nums2[j]) {
                temp[k] = nums1[i];
                i++;
            } else {
                temp[k] = nums2[j];
                j++;
            }
        }
        System.out.println(Arrays.toString(temp));

    }


    public static void merge3(int[] nums1, int m, int[] nums2, int n) {
        int index1 = m-1;
        int index2 = n-1;
        for (int i = m+n-1; i >=0 ; i--) {
            if (index2 <0) {
                return;
            } else if (index1< 0) {
                nums1[i] = nums2[index2];
                index2--;
            } else if(nums1[index1] <= nums2[index2]) {
                nums1[i] = nums2[index2];
                index2--;
            } else {
                nums1[i] = nums1[index1];
                index1--;
            }
        }
        System.out.println(Arrays.toString(nums1));


    }


    public static void main(String[] args) {
        int[] nums1 = {1,2,3,0,0,0};

        int[] nums2 = {2,5,6};
        merge3(nums1,3,nums2,3);
    }
}
